package com.gateway.core;

import java.nio.charset.Charset;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

/**
 * 编码
 * @author Simple
 * @date 2013-7-1 下午04:08:29
 * @Description TODO
 */
public class GatewayProtocolEncoder extends ProtocolEncoderAdapter {
  
  @SuppressWarnings("unused")
  private Logger log=Logger.getLogger(this.getClass());

  @SuppressWarnings("unused")
  private Charset charset;

  public GatewayProtocolEncoder(Charset charset) {
    this.charset=charset;
  }

  /**
   * 对LogicServer编码
   */
  public void encode(IoSession session, Object obj, ProtocolEncoderOutput out) throws Exception {
    MyProtocol myProtocol=(MyProtocol)obj;
    MyBuffer buffer=MyBuffer.allocate(100);
    buffer.put(myProtocol.getType());// type
    buffer.putInt(myProtocol.getBodyLength());// length
    buffer.put(myProtocol.getBodyData());// body
    buffer.flip();
    out.write(buffer.getIoBuffer());
  }
}
